﻿using System;
using Assets.Scripts.Net.Data.Model;
using UnityEngine;
using UnityEngine.UI;
using Assets.Scripts.UI.Panel;
using UnityEngine.EventSystems;

namespace Assets.Scripts.UI.Item
{
    public class AnsTextItem:UIBase<AnsTextItem>,IPointerEnterHandler,IPointerExitHandler
    {
        public Text AnsText;
        public Toggle Toggle;
        public Image WrongLable;//错误标签
        public Image RightLable;//正确标签

        private Action action { get; set; }//定义一个委托
        private AnsInfo ansInfo { get; set; }
        private int maxWordsNumber;//该选项所能显示的最大文字数量
        private bool isEnough;//是否能够显示所有ansInfo.content
        private AnswerDetailItem answerDetailItem;//显示所有选项内容
        private RectTransform Rect;//Item的RectTransform
        private bool isShowing;//是否正在显示
        private Vector2 vec2;//用于记录位置

        protected override UIType UIType
        {
            get { return UIType.Item;}
        }

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform,true);           
        }
#if TEST
        protected override void InitOnDestroy()
        {
            //ExamPanel.OnRandomAnswer -= OnRandomAnswer;
        }

        //private void OnRandomAnswer()
        //{
        //    System.Random random = new System.Random();
        //    int randomIndex = random.Next(0, 2);
        //    Toggle.isOn = randomIndex > 0;
        //}

        /// <summary>
        /// 选择该选项
        /// </summary>
        public void Chose()
        {
            Toggle.isOn = true;
        }
#endif


        public void Init(AnsInfo info, string label,Action action,ToggleGroup group,bool isCheckScore)
        {
#if TEST
            //ExamPanel.OnRandomAnswer += OnRandomAnswer;
#endif
            ansInfo = info;
            this.action = action;
            maxWordsNumber = 50;
            CheckLength(label);
            if (isEnough)
            {
                AnsText.text = label + ":" + info.content;
            } else
            {
                AnsText.text = label + ":" + info.content.Substring(0,(maxWordsNumber-3)>info.content.Length?info.content.Length: maxWordsNumber - 3) +"...";
            }            

            //测试使用
            //if(info.isright==1){
            //    AnsText.color=Color.green;
            //}
            if (group!=null)
            {
                Toggle.group = group;
            }
            Toggle.interactable = !isCheckScore;
            if (isCheckScore)
            {
                ShowToggleLabel();
                Toggle.isOn = info.ischoose;
                //Debug.Log("info.ischoose:" + info.ischoose);
                //将正确答案标记为绿色
                if (info.isright == 1)
                {
                    AnsText.color = new Color(0,0.5f,0);
                }
                if (info.ischoose)//如果答案被选择
                {
                    if (info.isright==1)//答案是正确的
                    {
                        ShowRightLable();
                    }
                    else//答案是错误的
                    {
                        ShowWrongLable();
                        AnsText.color=new Color(0.5f,0,0);
                    }
                }                          
            }
            else
            {
                ShowToggleLabel();
                Toggle.onValueChanged.AddListener(x =>
                {
                    ansInfo.ischoose = x;
                    if (action != null) action();
                });
            }
            Rect = gameObject.GetComponent<RectTransform>();
        }

        private void ShowRightLable()   
        {
            RightLable.gameObject.SetActive(true);
            WrongLable.gameObject.SetActive(false);
            Toggle.gameObject.SetActive(false);
        }

        private void ShowWrongLable()
        {
            RightLable.gameObject.SetActive(false);
            WrongLable.gameObject.SetActive(true);
            Toggle.gameObject.SetActive(false);
        }

        private void ShowToggleLabel()
        {
            RightLable.gameObject.SetActive(false);
            WrongLable.gameObject.SetActive(false);
            Toggle.gameObject.SetActive(true);
        }

        /// <summary>
        /// 检测AnsText是否能够显示所有的ansInfo.content
        /// </summary>
        private void CheckLength(string label)
        {
            if (ansInfo.content!= null){
                if (ansInfo.content.Length >= maxWordsNumber)
                {
                    isEnough = false;
                    answerDetailItem = ExamPanel._Instance.transform.Find("AnswerDetailItem").GetComponent<AnswerDetailItem>();
                }
                else
                {
                    isEnough = true;
                }
            }
            else
            {
                isEnough = true;
            }
                
        }

        private void Update()
        {
            if (isShowing)
            {
                RefreshAnswerDetailItemPosition();
            }
        }

        /// <summary>
        /// 刷新AnswerDetailItem的位置
        /// </summary>
        private void RefreshAnswerDetailItemPosition()
        {
            vec2 = Rect.position;
            vec2 = new Vector2(vec2.x - Rect.rect.width / 2, vec2.y - Rect.rect.height / 2);
            answerDetailItem.Show(vec2, ansInfo.content, Rect.rect.height);
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            if (isEnough) return;
            isShowing = true;
            RefreshAnswerDetailItemPosition();
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            if (isEnough)return;
            isShowing = false;
            answerDetailItem.Behind();
        }
    }
}